# encoding: utf-8
#Interface Graphique temporaire: elle permettra de choisir les controleurs MIDI et faire un mix du signal.
#import pyo
import wx
from srvr2 import *
# encoding: utf-8
from pyo import *

from pyolib._wxwidgets import VuMeter, ControlSlider, BACKGROUND_COLOUR
COLOURS = ["#000000", "#FF0000", "#0000FF", "#00FF00", "#FF00FF", "#FFFF00", "#00FFFF"]


class MainFrame(wx.Frame):
    def __init__(self, parent, id=-1, title="ELE", pos=(260,260), size=(1300,260)):
        wx.Frame.__init__(self, parent, id, title, pos, size)
        self.SetMinSize((526, 260))
        self.path = ""
        #self.sndProcessing = ["delay", "distortion", "frequency shift"]

        self.audioServer = Audio_Server()

        self.fxs = MFX()

        menubar = wx.MenuBar()
        fileMenu = wx.Menu()


        fileMenu.Append(wx.ID_EXIT, "Quit\tCtrl+Q")
        menubar.Append(fileMenu, "File")
        #Menue to choose audio driver
        audioMenu= wx.Menu()
        #Menue to choose Midi driver midiMenu= wx.Menu()

        audioMenu.Append(10000, "Start\tCtrl+Enter", kind=wx.ITEM_CHECK)
        audioMenu.AppendSeparator()
        self.names, default = self.audioServer.getAudioDriverNames()
        for i, name in enumerate(self.names):
            audioMenu.Append(i+9000, name, kind=wx.ITEM_RADIO)
            if name == default:
                audioMenu.Check(i+9000, True)
            self.Bind(wx.EVT_MENU, self.changeAudioDriver, id=i+9000)
        menubar.Append(audioMenu, "Audio")

        self.SetMenuBar(menubar)

        self.Bind(wx.EVT_MENU, self.onQuit, id=wx.ID_EXIT)
        self.Bind(wx.EVT_CLOSE, self.onQuit)
        self.Bind(wx.EVT_MENU, self.onStartStop, id=10000)

        self.panel = wx.Panel(self)
        self.panel.SetBackgroundColour("#000000")
        self.box = wx.BoxSizer(wx.VERTICAL)

        self.controlPanel = ControlPanel(self.panel, self.audioServer, self.fxs)#self.audioServer
        self.box.Add(self.controlPanel, 0, wx.EXPAND|wx.ALL, 5)

        self.panel.SetSizer(self.box)

        self.Show()

        #self.effes=MFX()
        #src2= MFX()
        #src=SfPlayer(SNDS_PATH + "/transparent.aif", speed=1, loop=True, offset=0, interp=2, mul=1, add=0)
        #src2=MFX()
        #effes.addInput(0, src)
        #effes.addEffect(0, 'distortion')
        #effes.setAmp(0, 0,0.01)
        #audioServer.gui()



    def onStartStop(self, evt):
        if evt.GetInt() == 1:
            self.audioServer.start()
        else:
            self.audioServer.stop()

    def changeAudioDriver(self, evt): #***
        name = self.names[evt.GetId()-9000]
        self.audioServer.changeAudioDriver(name)

    def onQuit(self, evt):
        self.audioServer.stop()
        self.Destroy()

class ControlPanel(wx.Panel):
    def __init__(self, parent, audioServer, fxs):
        wx.Panel.__init__(self, parent)
        self.SetBackgroundColour("#000000")
        self.audioServer= audioServer   
        self.fxs=MFX()

        self.box = wx.BoxSizer(wx.HORIZONTAL)

        # Boite verticale pour le gain (label en haut et slider en bas)
        self.gainBox = wx.BoxSizer(wx.VERTICAL)  
 
        self.masterText = wx.StaticText(self, label="Master Gain (dB)")
        self.masterText.SetForegroundColour((255,255,255))
        
        self.masterGain = ControlSlider(self, -60, 6, init=-9, outFunction=self.setMasterGain)
        self.gainBox.Add(self.masterText,1)
        self.gainBox.Add(self.masterGain, 1, wx.EXPAND)

        self.SetSizer(self.box)
        
        #**********vuMetre*****************************
        self.meter = VuMeter(self, numSliders=2)

        self.audioServer.s._server.setAmpCallable(self.meter)

        self.box.Add(self.gainBox, 100, wx.EXPAND|wx.ALL, 20)
        self.box.Add(self.meter, 10, wx.TOP, 40)
        
        self._inToggle = wx.ToggleButton(self,label='INPUT', pos=(100,00), size=wx.DefaultSize)
        self._inToggle.Bind(wx.EVT_TOGGLEBUTTON, self.handleInput)

        self._delayTog = wx.ToggleButton(self,label='delay', pos=(200,00), size=wx.DefaultSize)
        self._delayTog.Bind(wx.EVT_TOGGLEBUTTON, self.handleDelay)

        self._distTog = wx.ToggleButton(self,label='distortion', pos=(300,00), size=wx.DefaultSize)
        self._distTog.Bind(wx.EVT_TOGGLEBUTTON, self.handleDisto)

        self._fresTog = wx.ToggleButton(self,label='frequency shift', pos=(400,00), size=wx.DefaultSize)
        self._fresTog.Bind(wx.EVT_TOGGLEBUTTON, self.handleFrs)
    
    def setMasterGain(self, x):
        x = pow(10, x * 0.05)
        #modifie l<amplitude globale du serveur audio
        self.audioServer.s.setAmp(x)

    """" Active l'entree du signal"""
    def handleInput(self, evt):
        print ("input on off")
        print evt.GetInt()
        if evt.GetInt() == 1:
            self.fxs._in
        else:
            return 0

    """" Active le delay"""
    def handleDelay(self, evt):
        print ("delay on off")
        print evt.GetInt()
        if evt.GetInt() == 1:
            self.fxs.addInput(0,Sig(self.fxs._in,mul=100))
            self.fxs.addEffect(0,'delay')
            self.fxs.setAmp(0,0,0.78)
            
        else:
            self.fxs.setAmp(0,0,0.0)
            return 0

    """" Active la disto"""
    def handleDisto(self, evt):
        print ("distortion on off")
        print evt.GetInt()
        if evt.GetInt() == 1:
            self.fxs.addInput(1,Sig(self.fxs._in,mul=100))
            self.fxs.addEffect(1, 'distortion')
            self.fxs.setAmp(1,1,0.52)
            
        else:
            self.fxs.setAmp(1,1,0.0)
            return 0

    """" Active le FreqShift"""
    def handleFrs(self, evt):
        print ("Frs on off")
        print evt.GetInt()
        if evt.GetInt() == 1:
            self.fxs.addInput(2,Sig(self.fxs._in,mul=100))
            self.fxs.addEffect(2, 'frequency shift')
            self.fxs.setAmp(2,2,0.52)
            
        else:
            self.fxs.setAmp(2,2,0.0)
            return 0





        

if __name__=="__main__" :
    
    import pyo
    app=wx.PySimpleApp()
    f=MainFrame(None)
    app.MainLoop()



#    import pyo
#    m= MainFrame()


